package com.hermes.action.json;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.item.ItemDO;
import com.hermes.biz.common.model.item.ItemUserRelationDO;
import com.hermes.biz.common.model.result.ItemResultDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.item.UserItemRelationService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.LoginHelperUtil;
import com.hermes.common.util.ParameterParserUtil;

public class AsynSetMyLikeItem  extends AsynBaseAction{

	private static final long serialVersionUID = 1L;

	private UserItemRelationService userItemRelationService;
	
	private UserService userService;
	
	private ItemService itemService;

	public String result;

	public String isRedirect;

	private Logger log = Logger.getLogger(AsynSetMyLikeItem.class);
	
	@Override
	public String execute() throws Exception {
		
		long userId =7;
		long itemIdNum=0;
		
		String itemId = ParameterParserUtil.getParameter("itemid");
		
		if(StringUtil.isNotBlank(itemId)&&StringUtil.isNumOnly(itemId)){
			itemIdNum = Long.parseLong(itemId);
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		LoginHelperUtil loginHelper = new LoginHelperUtil(request);
		if(loginHelper.hasLogined()){
			log.error("user-log-in-report-in-detail");
			String username = loginHelper.getCurrentUserName();
			UserResultDO userResultDO = userService.getUserInfoByUserName(username);
			if(userResultDO.isSuccess()&& userResultDO.getModule()!=null){
				userId = userResultDO.getModule().getUserId();
			}
		}
		else{
			result="true";
			isRedirect="false";
			ItemResultDO resDO = itemService.getSingleItemByItemId(itemIdNum);
			if(resDO!=null && resDO.isSuccess()){
				ItemDO itemDO = resDO.getModule();
				itemDO.setBelovedNum(itemDO.getBelovedNum()+1);
			}
			return SUCCESS;
		}
		
		ItemUserRelationDO itemUserRelationDO = new ItemUserRelationDO();
		itemUserRelationDO.setUserId(userId);
		itemUserRelationDO.setInterestedItemId(itemIdNum);
	
		try{	
			if(this.userItemRelationService.hasUserInterestedItem(userId, itemIdNum)){
				
				boolean delRes = this.userItemRelationService.deleteUserInterestedItem(userId, itemIdNum);
				if(!delRes){
					result="false";
					isRedirect="false";
				}
				result="true";
				isRedirect="false";
				return SUCCESS;
			}
			this.userItemRelationService.insertUserInterestedItem(itemUserRelationDO);
		}
		catch(Exception e){
			result="false";
			isRedirect="false";
			return SUCCESS;
		}
		result="true";
		isRedirect="false";
		return SUCCESS;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getIsRedirect() {
		return isRedirect;
	}

	public void setIsRedirect(String isRedirect) {
		this.isRedirect = isRedirect;
	}

	public UserItemRelationService getUserItemRelationService() {
		return userItemRelationService;
	}

	public void setUserItemRelationService(UserItemRelationService userItemRelationService) {
		this.userItemRelationService = userItemRelationService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

}
